﻿Imports System.Data.SqlClient

Public Class adm_productpurchaseupdate
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim strErr As String
                Dim id, title, purchaseCategory, purchaseSubCategory, description, country, state, city, delplace, purchaseNum, expPrice, linkMan As String
                Dim validdate
                Dim gender As Boolean
                strErr = ""
                If (Request("productPurchaseSubmit") <> "" And Request("productPurchaseSubmit") <> Nothing) Then
                    If (Request("c_purchase_title") = "" Or Request("c_purchase_title") = Nothing) Then
                        Me.c_purchase_title_label.Text = "please enter title！"
                        Exit Sub
                    Else
                        Me.c_purchase_title_label.Text = ""
                    End If
                    If (Request("c_linkman") = "" Or Request("c_linkman") = Nothing) Then
                        Me.c_linkman_label.Text = "please enter contact！"
                        Exit Sub
                    Else
                        Me.c_linkman_label.Text = ""
                    End If
                    id = Request("purchaseId")
                    title = Request("c_purchase_title")
                    purchaseCategory = Request("c_purchase_category")
                    If (Request("c_purchase_subcategory") <> Nothing And Request("c_purchase_subcategory") <> "") Then
                        purchaseSubCategory = Request("c_purchase_subcategory")
                    Else
                        purchaseSubCategory = ""
                    End If
                    description = Request("c_description")
                    country = Request("country")
                    state = Request("state")
                    city = Request("city")
                    delplace = Request("c_delplace")
                    purchaseNum = Request("c_purchaseNum")
                    expPrice = Request("c_expprice")
                    If (Request("c_validdate") = Nothing Or Request("c_validdate").Trim = "") Then
                        validdate = "1753-01-01"
                    Else
                        validdate = Request("c_validdate")
                    End If
                    linkMan = Request("c_linkman")
                    If (Request("gender") = "0") Then
                        gender = False
                    ElseIf (Request("gender") = "1") Then
                        gender = True
                    End If
                    ps.strSql = "update purchase set title=@title, category_id=@category,sub_category_id=@subCategory,detailinfo=@detailInfo," & _
                                "country=@country,state=@state,city=@city,delplace=@delplace,purchasenum=@purchasenum,expprice=@expprice," & _
                                "validdate=@validdate,linkman=@linkman,gender=@gender"
                    ps.parameters.Clear()
                    ps.addParameter("id", id)
                    ps.addParameter("title", title)
                    ps.addParameter("category", purchaseCategory)
                    ps.addParameter("subCategory", purchaseSubCategory)
                    ps.addParameter("detailInfo", description)
                    ps.addParameter("country", country)
                    ps.addParameter("state", state)
                    ps.addParameter("city", city)
                    ps.addParameter("delplace", delplace)
                    ps.addParameter("purchasenum", purchaseNum)
                    ps.addParameter("expprice", expPrice)
                    ps.addParameter("validdate", validdate)
                    ps.addParameter("linkman", linkMan)
                    ps.addParameter("gender", gender)
                    objfind.es(ps, strErr)
                    Response.Redirect("adm_purchaseManagement.aspx?uid=" & Request("uid"))
                ElseIf (Request("c_purchase_category") = Nothing Or Request("c_purchase_category") = "") Then
                    id = Request("purchaseId")
                    ps.strSql = "select * from purchase where id = @id"
                    ps.parameters.Clear()
                    ps.addParameter("id", id)
                    Dim conn As SqlConnection = objfind.getConn()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (objrd.Read()) Then
                        Me.purchaseId.Value = id
                        Me.c_purchase_title.Text = objrd("title").ToString
                        Me.c_description.Text = objrd("detailinfo").ToString
                        Me.country_code.Text = objrd("country").ToString
                        Me.state_code.Text = objrd("state").ToString
                        Me.city_code.Text = objrd("city").ToString
                        Me.c_delplace.Text = objrd("delplace").ToString
                        Me.c_purchaseNum.Text = objrd("purchasenum").ToString
                        Me.c_expprice.Text = objrd("expprice").ToString
                        Me.c_validdate.Text = objrd("validdate").ToString
                        Me.c_linkman.Text = objrd("linkman").ToString
                        If (objrd("gender").ToString.ToLower() = "false") Then
                            Me.mgender.Checked = True
                        ElseIf (objrd("gender").ToString.ToLower() = "true") Then
                            Me.fgender.Checked = True
                        End If
                        purchaseCategory = objrd("category_id")
                        purchaseSubCategory = objrd("sub_category_id")
                        ps.strSql = "select * from category where lang_flag='en'"
                        ps.parameters.Clear()
                        objrd = objfind.dr(ps, strErr, conn, objrd)
                        c_purchase_category.Items.Clear()
                        c_purchase_category.Items.Add(New ListItem("select category", ""))
                        While (objrd.Read())
                            c_purchase_category.Items.Add(New ListItem(
                            objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                        End While
                        c_purchase_category.SelectedValue = purchaseCategory
                        ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='en'"
                        ps.parameters.Clear()
                        ps.addParameter("categoryId", purchaseCategory)
                        objrd = objfind.dr(ps, strErr, conn, objrd)
                        c_purchase_subcategory.Items.Clear()
                        c_purchase_subcategory.Items.Add(New ListItem("select sub category", ""))
                        While (objrd.Read)
                            c_purchase_subcategory.Items.Add(New ListItem(
                                objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
                        End While
                        c_purchase_subcategory.SelectedValue = purchaseSubCategory
                    End If
                    objfind.closeConn(conn)

                End If
            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub c_purchase_category_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles c_purchase_category.SelectedIndexChanged
        Dim categoryId, strErr As String
        strErr = ""
        categoryId = c_purchase_category.SelectedValue
        ps.strSql = "select * from sub_category where category_id=@categoryId and lang_flag='en'"
        ps.parameters.Clear()
        ps.addParameter("categoryId", categoryId)
        Dim conn As SqlConnection = objfind.getConn()
        objrd = objfind.dr(ps, strErr, conn, objrd)
        c_purchase_subcategory.Items.Clear()
        c_purchase_subcategory.Items.Add(New ListItem("select sub category", ""))
        While (objrd.Read())
            c_purchase_subcategory.Items.Add(New ListItem(
            objrd("Sub_Category_Name").ToString(), objrd("Sub_Category_ID").ToString()))
        End While
        c_purchase_category.Items.Clear()
        c_purchase_category.Items.Add(New ListItem("select category", ""))
        ps.strSql = "select * from category where lang_flag='en'"
        ps.parameters.Clear()
        objrd = objfind.dr(ps, strErr, conn, objrd)
        While (objrd.Read())
            c_purchase_category.Items.Add(New ListItem(
            objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
        End While
        objfind.closeConn(conn)
        c_purchase_category.SelectedValue = categoryId
    End Sub

End Class